<?php 
class onekey_message_home 
{ 
	function onekey_message_main() 
	{ 
		global $mysql_conn,$v_file;
		$bot_id = $_GET['bot_id'];
		$sql = "SELECT * from `bot` WHERE `bot_id` = '".$bot_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$bot_server_id = $row['bot_server_id'];
		$sql = "SELECT * from `server` WHERE `server_id` = '".$bot_server_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$server_url = $row['server_url'];
		//获取好友数据
		$url = $server_url."openwx/get_friend_info?client=".$bot_id; 
		$html = file_get_contents($url);
		$contact = json_decode($html);
		foreach ($contact as $key=>$value)
		{
			$friend_account = $value->{'account'};
			$friend_name = $value->{'name'};
			$friend_id = $value->{'id'};
			$friend_displayname = $value->{'displayname'};
			$friend_avatar_url = $server_url."openwx/get_avatar?id=".$friend_id."&client=".$bot_id; 
			$friend_category = $value->{'category'};
			if ($friend_category != "公众号") {
				$friend_list.= '<tr><td class="client-avatar"><img alt="image" src="'.$friend_avatar_url.'"> </td>';
				$friend_list.= '<td><a data-toggle="tab" href="#" name="'.$friend_name.'" class="client-link">'.$friend_name.'</a></td>';
				$friend_list.= '<td> '.$friend_displayname.'</td>';
				$friend_list.= '<td class="contact-type"><i class="fa fa-comments"> </i></td>';
				$friend_list.= '<td> '.$friend_account.'</td>';
				$friend_list.= '<td class="client-status"><input type="checkbox" name="onekey_message_usename" value="'.$friend_id.'" > </td></tr>';
			}	
		}
		//获取群组数据
		$url = $server_url."openwx/get_group_info?client=".$bot_id;
		$html = file_get_contents($url);
		$contact = json_decode($html);
		foreach ($contact as $key=>$value)
		{
			$group_displayname = $value->{'displayname'};
			$group_id = $value->{'id'};
			$group_member = $value->{'member'};
			$group_member_num = count($group_member);
			$group_avatar_url = $server_url."openwx/get_avatar?id=".$group_id."&client=".$bot_id; 			
			$group_list.= '<tr><td class="client-avatar"><img alt="image" src="'.$group_avatar_url.'"> </td>';
			$group_list.= '<td><a data-toggle="tab" href="weibot_friend.php?m=view$weibot_id='.$client.'&friendid='.$group_id.'" class="client-link">'.$group_displayname.'</a></td>';
			$group_list.= '<td></td><td class="contact-type"><i class="fa fa-comments"> </i></td>';
			$group_list.= '<td>成员：'.$group_member_num.' 人</td>';
			$group_list.= '<td class="client-status"><input type="checkbox" name="onekey_message_usename" value="'.$group_id.'"  onClick="onekey_message_uselist()" > </td></tr>';						
		}
		require($v_file);
	}
	function onekey_message_do() 
	{
		global $mysql_conn,$v_file;
		$bot_id = $_POST['bot_id'];
		$onekey_message = urlencode($_POST['onekey_message']);
		$onekey_message_list = $_POST['onekey_message_list'];
		//这里是循环发送数据
		//获取服务器连接
		$sql = "SELECT * from `bot` WHERE `bot_id` = '".$bot_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$bot_server_id = $row['bot_server_id'];
		$sql = "SELECT * from `server` WHERE `server_id` = '".$bot_server_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$server_url = $row['server_url'];
		//根据不同的好友构造不同的url进行发送数据
		$onekey_message_list_arr = explode(',',$onekey_message_list); 
		$count=count($onekey_message_list_arr); 
		for($i=0;$i<$count;$i++) 
		{ 
			
			$displayid = $onekey_message_list_arr[$i]; 
			$displayid_type = substr($displayid , 0 , 2);
			if ($displayid_type =="@@") {
				$url = $server_url."openwx/send_group_message?id=".$displayid."&content=".$onekey_message."&client=".$bot_id;
			} else {
				$url = $server_url."openwx/send_friend_message?id=".$displayid."&content=".$onekey_message."&client=".$bot_id;
			}
			$html = file_get_contents($url);
		}
		echo "2";
	}
} 
/* End of file democontroller.php */ 